package com.dingyh.shopwebbackground.controller;


import com.dingyh.shopwebbackground.entity.Address;
import com.dingyh.shopwebbackground.entity.Advice;
import com.dingyh.shopwebbackground.service.AdviceService;
import com.dingyh.shopwebbackground.util.CommonResult;
import com.dingyh.shopwebbackground.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dingyuhan
 * @since 2023-02-17
 */
@RestController
@CrossOrigin
@RequestMapping("/advice")
public class AdviceController {

    @Autowired
    AdviceService adviceService;

    @RequestMapping("getAllWrite")
    public CommonResult<Object> getAllWrite(@RequestParam(defaultValue = "1",required = true )int pageIndex, @RequestParam(defaultValue = "10",required = true )int pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        Map<String,Object> map = new HashMap<>();
        List<Advice> advice = adviceService.getAllWrite(map);
        PageInfo<Advice> pageInfo =new PageInfo<>(advice);
        return new CommonResult(200,"",pageInfo);
    }

    /**
     * 删除资讯
     * @param id
     * @return
     */
    @RequestMapping("removeWrite")
    public CommonResult<Object> removeWrite(String id){
        if(adviceService.removeWrite(id)){
            return new CommonResult(200, "删除成功！", null);
        }else{
            return new CommonResult(500, "删除失败！", null);
        }
    }


    @RequestMapping("updateWrite")
    public CommonResult<Object> updateWrite(Advice advice){
        if(adviceService.updateById(advice)){
            return new CommonResult(200, "编辑成功！", null);
        }else{
            return new CommonResult(500, "编辑失败！", null);
        }
    }

    /**
     * 通过id查找资讯
     * @param id
     * @return
     */
    @RequestMapping("selectWriteById")
    public  CommonResult<Object> selectWrite(String id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        List<Advice> advice = adviceService.selectWriteById(map);
        if(!advice.isEmpty() ){
            return new CommonResult(200,"",advice);
        }else {
            return new CommonResult(500,"您查找的资讯不存在！");
        }
    }

    /**
     * 通过资讯名模糊查找
     * @param title
     * @return
     */
    @RequestMapping("selectWriteByName")
    public  CommonResult<Object> selectWriteByName(String title){
        Map<String,Object> map = new HashMap<>();
        map.put("title",title);
        List<Advice> advice = adviceService.selectWriteByName(map);
        if(!advice.isEmpty() ){
            return new CommonResult(200,"",advice);
        }else {
            return new CommonResult(500,"您查找的资讯不存在！");
        }
    }

    /**
     * 新建咨询
     * @param title
     * @param detail
     * @return
     */
    @RequestMapping("addWrite")
    public CommonResult<Object> addWrite(String title,String detail){
        Advice advice = Advice.builder()
                .id(UUIDUtil.generator())
                .title(title)
                .detail(detail)
                .timeadd(System.currentTimeMillis())
                .build();
        boolean result = adviceService.save(advice);
        if (result) {
            return new CommonResult(200, "提交成功！", null);
        } else {
            return new CommonResult(500, "提交失败！", null);
        }
    }
}

